import { JKManager } from "../../JKManager";
import { LocalizedLabel } from "../../Language/LocalizedLabel";
import { TipViewBase } from "../TipViewBase";

const { ccclass, property } = cc._decorator;

/**
 * 公共弹窗组件
 */
@ccclass
export class CommonTipView extends TipViewBase {
    private _ndCtn: cc.Node;
    private _labMsg: LocalizedLabel;
    private _hiding: boolean;

    onLoad() {
        super.onLoad()
        cc.log(`${cc.js.getClassName(this)} onLoad`)

        this._ndCtn = cc.find(`ndCtn`, this.node)
        this._labMsg = cc.find(`ndCtn/ndMsg/labMsg`, this._ndCtn).getComponent(LocalizedLabel)
    }

    init(data: { msg: string; }): void {
        cc.log(`${cc.js.getClassName(this)} init data = `, data)

        let msg = data.msg

        // Ui
        let dataUi = {}
        this._dataUpdateTool.put(JKManager.Const.CommonTipView.CpntName.Ui, dataUi)
        this._labMsg.textKey = msg
    }

    start(): void {
        cc.log(`${cc.js.getClassName(this)} start`)

        // Ui
        this._dataUpdateTool.registCpntUpdateWithName(JKManager.Const.CommonTipView.CpntName.Ui, this._updateCpntUi.bind(this))
        this._updateCpntUi(this._dataUpdateTool.getCpntDataByName(JKManager.Const.CommonTipView.CpntName.Ui), true)
    }

    update(dt: number): void {
        super.update(dt)
    }

    close(): void {
        cc.log(`${cc.js.getClassName(this)} close`)

        JKManager.UiManager.closeTipView()
    }

    onDestroy(): void {
        cc.log(`${cc.js.getClassName(this)} onDestroy`)

    }

    // ====================================================================================
    // ===================================== 内部接口 ======================================
    // ====================================================================================

    /**
     * 刷新组件 LiveVideoPlayer
     * @param data 数据对象
     * @param immediately 是否同步刷新
     */
    _updateCpntUi(data: unknown, immediately: boolean = false): void {
        cc.log(`${cc.js.getClassName(this)} _updateCpntUi data = `, data)

        this._taskQueueTool.addCmd({
            cmd: JKManager.Const.CommonTipView.Cmd.UpdateCpntUi,
            params: {},
            call: (cmd, params) => {
                cc.log(`${cc.js.getClassName(this)} Cmd.UpdateCpntUi params = `, params)

                this._taskQueueTool.doneCmd(cmd)
            }
        }, immediately)
    }

    // ====================================================================================

    // ====================================================================================
    // ===================================== 外部接口 ======================================
    // ====================================================================================
}